home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: cpmorris@popd.ix.netcom.com (Chris Morris)
- Newsgroups: comp.lang.c++
- Subject: Friends....
- Date: Tue, 02 Jan 1996 00:08:12 GMT
- Organization: Netcom
- Message-ID: <4c9srh$g2l@ixnews3.ix.netcom.com>
- NNTP-Posting-Host: ix-stp-fl4-10.ix.netcom.com
- X-NETCOM-Date: Mon Jan 01 4:05:05 PM PST 1996
- X-Newsreader: Forte Free Agent 1.0.82
-
- I am a beginner with C++ and have a question concerning the
- declaration of 'friend' functions. I am working with Windows
- programming, but this question doesn't apply to Windows, it is a C++
- question.
-
- This is the class code I am confused about
-
-
- class TLine : public TPoints {
- public:
- TLine(int penSize = 1) : TPoints(10, 0, 10)
- {
- PenSize = penSize;
- }
-
- int QueryPen() const
- {
- return PenSize;
- }
-
- int QueryPen(int penSize);
-
- // The == operator must be defined for the container class, even
- if unused
- bool operator ==(const TLine& other) const
- {
- return &other == this;
- }
-
- friend ostream& operator <<(ostream& os, const TLine& line);
- friend istream& operator >>(istream& is, TLine& line);
-
- protected:
- int PenSize;
- };
-
-
- Why are the two operator functions (<< and >>) declared as friends?
- Is it because the original declarations are not virtual and cannot be
- overridden? I understand that these function have to be modified
- because they deal with a user-defined type. But look at the
- definitions of the functions
-
-
- ostream&
- operator <<(ostream& os, const TLine& line)
- {
- // Write the number of points in the line
- os << line.GetItemsInContainer();
-
- // Write the pen size
- os << ' ' << line.PenSize;
-
- // Get an iterator for the array of points
- TPointsIterator j(line);
-
- // While the iterator is valid (i.e. we haven't run out of points)
- while(j)
- // Write the point from the iterator and increment the array.
- os << j++;
-
- os << '\n';
- // return the stream object
- return os;
- }
-
- istream&
- operator >>(istream& is, TLine& line)
- {
- unsigned numPoints;
-
- is >> numPoints;
-
- is >> line.PenSize;
-
- while (numPoints--) {
- TPoint point;
- is >> point;
- line.Add(point);
- }
- // return the stream object
- return is;
- }
-
-
- This is the confusing part. How can the << and >> be used in the
- definitions of << and >>? Is seems like the new << and >> friend
- functions are just defining their own implementations for handling the
- user-defined type. This is overloading, isn't it?
-
- I have, and am nearly finished with "The C++ Programming Language" by
- Bjarne Stroustrup. When I look at the friend function section he says
- that friends are used so that two classes can have functions in
- common. The function is able to access the private part of each
- class. But he says nothing about using a 'friend' declaration to do
- what this example is doing. Help?
-
- Thanks in advance...
-
-
- -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-
- Chris Morris (cpmorris@ix.netcom.com)
- P. O. Box 574
- Clearwater, FL 34617-0574
-
-
- Reality is a crutch for people who can't deal with science fiction . . .
- -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-
-